﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace test
{
    public partial class FormRegister : Form
    {
        //User user = new User();
        public FormRegister()
        {
            InitializeComponent();
        }
        //注册的取消按钮
        private void btn_cancle_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btn_register_Click(object sender, EventArgs e)
        {
            if (CheckInput())
            {
                if (Register())
                {
                    Form1 form1 = new Form1();
                    //传值
                    //form1.user = user;
                    form1.Show();
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("注册失败！");
                }
            }
        }
        //非空验证
        private bool CheckInput()
        {
            if (text_username.Text.Trim() == "" || text_password.Text.Trim() == ""||text_password2.Text.Trim()=="")
            {
                MessageBox.Show("请输入用户名和密码！");
                return false;
            }
            if (text_password.Text.Trim() != text_password2.Text.Trim())
            {
                MessageBox.Show("两次输入密码不一样！");
                return false;
            }
            return true;
        }
        ///登录
        public bool Register()
        {
            bool flag = false;
            String username = text_username.Text.Trim();
            String password = text_password.Text.Trim();
            DBHelper dBHelper = new DBHelper();
            try
            {
                //1.创建sql语句
                String sql = String.Format("insert into user(username,password) value('{0}','{1}')", username, password);
                //2.command工具
                MySqlCommand cmd = new MySqlCommand(sql, dBHelper.Connection);
                //3.打开连接
                dBHelper.OpenConnection();
                //4.执行
                int result = cmd.ExecuteNonQuery();
                //5.判断
                if (result == 1)
                {
                    MessageBox.Show("注册成功！", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    flag = true;
                    this.Close();
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("注册失败：" + e.Message);
            }
            finally
            {
                dBHelper.CloseConnection();
            }
            return flag;
        }

        private void link_Login_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Form1 form = new Form1();
            form.Show();
            this.Hide();
        }
    }
}
